<template>
  <view class="search-results-page">
    <!-- 顶部搜索栏 -->
    <view class="search-header">
      <view class="search-input-wrapper">
        <view class="search-input">
          <text class="search-icon">🔍</text>
          <input 
            class="input-field" 
            placeholder="搜索商品" 
            placeholder-class="placeholder"
            v-model="searchKeyword"
            @input="onInputChange"
          />
        </view>
        <text class="search-btn" @click="performSearch">搜索</text>
      </view>
    </view>
    
    <!-- 搜索结果 -->
    <view class="results-container">
      <view v-if="loading" class="loading">
        <text>搜索中...</text>
      </view>
      
      <view v-else-if="searchResults.length === 0" class="no-results">
        <text>没有找到相关商品</text>
      </view>
      
      <view v-else class="results-list">
        <view 
          class="product-item" 
          v-for="(product, index) in searchResults" 
          :key="product.id"
          @click="onProductClick(product)"
        >
          <view class="product-image">
            <image 
              :src="getFirstImage(product.imageUrls)" 
              mode="aspectFill"
              class="image"
            />
          </view>
          <view class="product-info">
            <view class="product-name">{{ product.name }}</view>
            <view class="product-brand">品牌名称 {{ product.brandName }}</view>
            <view class="product-category">分类 {{ product.categoryName }}</view>
            <view class="product-unit">商品单位 {{ product.unit }}</view>
          </view>
        </view>
      </view>
      
      <!-- 加载更多 -->
      <view v-if="hasMore && !loading" class="load-more" @click="loadMore">
        <text>加载更多</text>
      </view>
    </view>
  </view>
</template>

<script>
import { get } from '@/utils/request.js';

export default {
  data() {
    return {
      searchKeyword: '', // 搜索关键词
      searchResults: [], // 搜索结果
      loading: false, // 加载状态
      pageNum: 1, // 当前页码
      pageSize: 10, // 每页数量
      hasMore: true, // 是否有更多数据
      total: 0 // 总数量
    }
  },

  onLoad(options) {
    if (options.keyword) {
      this.searchKeyword = decodeURIComponent(options.keyword);
      this.performSearch();
    }
  },

  methods: {
    // 输入内容变化
    onInputChange(e) {
      this.searchKeyword = e.detail.value;
    },
    
    // 执行搜索
    performSearch() {
      if (!this.searchKeyword.trim()) {
        uni.showToast({ title: '请输入搜索内容', icon: 'none' });
        return;
      }
      this.pageNum = 1;
      this.searchResults = [];
      this.hasMore = true;
      this.searchProducts();
    },
    
    // 搜索商品
    async searchProducts() {
      try {
        this.loading = true;
        const res = await get('/transaction/spu/list', {
          name: this.searchKeyword, // 后端 SpuVO 的查询字段是 name
          pageNum: this.pageNum,
          pageSize: this.pageSize
        });
    
        console.log('搜索结果响应:', res);
    
        if (res.code === 200) {
          const results = res.rows || [];
          if (this.pageNum === 1) {
            this.searchResults = results;
          } else {
            this.searchResults = [...this.searchResults, ...results];
          }
		console.log(this.searchResults)
          this.total = res.total || 0;
          this.hasMore = this.searchResults.length < this.total;
        } else {
          console.error('搜索失败:', res.msg);
          uni.showToast({ title: '搜索失败', icon: 'none' });
        }
      } catch (err) {
        console.error('搜索请求失败:', err);
        uni.showToast({ title: '网络错误', icon: 'none' });
      } finally {
        this.loading = false;
      }
    },

    
    // 加载更多
    loadMore() {
      if (this.hasMore && !this.loading) {
        this.pageNum++;
        this.searchProducts();
      }
    },
    
    // 点击商品
    onProductClick(product) {
      console.log('点击了商品:', product);
      uni.navigateTo({ url: `/pages/product_detail/product_detail?id=${product.id}` });
    },
    
    // 获取第一张图片
    getFirstImage(imageUrls) {
      if (!imageUrls) return '/static/default-product.png';
      const urls = imageUrls.split(',');
      return urls[0] || '/static/default-product.png';
    }
  }
}
</script>

<style scoped>
.search-results-page {
  background-color: #f5f5f5;
  min-height: 100vh;
}

/* 搜索头部 */
.search-header {
  background-color: #fff;
  padding: 20rpx 30rpx;
  border-bottom: 1rpx solid #e0e0e0;
}

.search-input-wrapper {
  display: flex;
  align-items: center;
}

.search-input {
  flex: 1;
  display: flex;
  align-items: center;
  background-color: #f5f5f5;
  border-radius: 20rpx;
  padding: 20rpx 30rpx;
  margin-right: 20rpx;
}

.search-icon {
  font-size: 32rpx;
  color: #999;
  margin-right: 20rpx;
}

.input-field {
  flex: 1;
  font-size: 32rpx;
  color: #333;
  background-color: transparent;
}

.placeholder {
  font-size: 32rpx;
  color: #999;
}

.search-btn {
  font-size: 32rpx;
  color: #007aff;
  padding: 10rpx 20rpx;
}

/* 结果容器 */
.results-container {
  padding: 20rpx;
}

.loading, .no-results {
  text-align: center;
  padding: 100rpx 0;
  color: #999;
  font-size: 28rpx;
}

/* 商品列表 */
.results-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.product-item {
  background-color: #fff;
  border-radius: 10rpx;
  padding: 20rpx;
  display: flex;
  align-items: center;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}

.product-image {
  width: 120rpx;
  height: 120rpx;
  margin-right: 20rpx;
  border-radius: 8rpx;
  overflow: hidden;
}

.image {
  width: 100%;
  height: 100%;
}

.product-info {
  flex: 1;
}

.product-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
  line-height: 1.4;
}

.product-brand {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 8rpx;
}

.product-category {
  font-size: 24rpx;
  color: #999;
  margin-bottom: 8rpx;
}

.product-unit {
  font-size: 24rpx;
  color: #007aff;
}

/* 加载更多 */
.load-more {
  text-align: center;
  padding: 30rpx;
  color: #007aff;
  font-size: 28rpx;
  background-color: #fff;
  border-radius: 10rpx;
  margin-top: 20rpx;
}

/* 点击效果 */
.product-item:active {
  transform: scale(0.98);
}

.search-btn:active {
  opacity: 0.7;
}

.load-more:active {
  opacity: 0.7;
}
</style>
